#include "CWinState.h"
#include "CGame.h"
#include "CMainMenuState.h"
#include "CPrintBitmapToScreen.h"
#include "CSpaceStationState.h"
#include "Game Objects\CPlayer.h"
#include "Game Objects\CBase.h"
#include "CAnimationManager.h"
#include "CHowToPlayState.h"

CWinState::CWinState(void)
{
	m_nScreenWidth = CGame::GetInstance()->GetScreenWidth();
	m_nScreenHeight = CGame::GetInstance()->GetScreenHeight();
}

CWinState::CWinState(const CWinState&)
{

}

CWinState::~CWinState()
{

}

CWinState& CWinState::operator=(const CWinState&)
{
	return *this;
}

CWinState* CWinState::GetInstance(void)
{
	static CWinState instance;
	return &instance;
}

void CWinState::Enter(void)
{
	m_nCurrentSelection		=  0;
	m_nBackgroundImageID	= -1;
	m_nBackgroundMusicID	= -1;
	m_nMovementSFXID		= -1;
	m_nBitmapFontID			= -1;

	m_fVolume				= CGame::GetInstance()->GetMusicVolume();
	m_fSFXVolume			= CGame::GetInstance()->GetSFXVolume();

	m_nScreenWidth = CGame::GetInstance()->GetScreenWidth();
	m_nScreenHeight = CGame::GetInstance()->GetScreenHeight();
	//isFull = false;
	// Load sounds
	m_nMovementSFXID = CSGD_FModManager::GetInstance()->LoadSound("resource/Sound/SGP_MenuSelection.wav");
	m_nBackgroundMusicID = CSGD_FModManager::GetInstance()->LoadSound("resource/Sound/SGP_Course.mp3");

	// Load textures
	m_nBackgroundImageID = CSGD_TextureManager::GetInstance()->LoadTexture("Resource/Graphics/Art Assets/SGP_CSBackground.png");

	//Load Animation

	m_nPlayerDancingID = CAnimationManager::GetInstance()->Load("Resource/Graphics/Animations/WinDance.xml",D3DCOLOR_XRGB(34,190,34));

	if(!m_pPlayer) // If the player is not already present
	{
		m_pPlayer = CPlayer::GetInstance();
		m_pPlayer->SetPosX(0);
		m_pPlayer->SetPosY(200);
		m_pPlayer->SetVelX(0.0f);
		m_pPlayer->SetVelY(0.0f);
		m_pPlayer->SetType(CBase::OBJ_PLAYER);

		ObjectManager::GetInstance()->AddObject(m_pPlayer);
	}

}
bool CWinState::Input(void)
{
	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_LEFT) || CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_LEFT))
	{
		if(m_nCurrentSelection > 0)
			--m_nCurrentSelection;
		else
			m_nCurrentSelection = 1;

		CSGD_FModManager::GetInstance()->PlaySoundA(m_nMovementSFXID);
	}

	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_RIGHT)|| CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_RIGHT))
	{
		if(m_nCurrentSelection < 1)
			++m_nCurrentSelection;
		else
			m_nCurrentSelection = 0;

		CSGD_FModManager::GetInstance()->PlaySoundA(m_nMovementSFXID);
	}
	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_RETURN)|| CSGD_DirectInput::GetInstance()->JoystickButtonPressed(0))
	{
		
		if(m_nCurrentSelection ==0)
		{
			if(CHowToPlayState::GetInstance()->GetIsGuideRunning() == true)
			{
				CGame::GetInstance()->ChangeState(CMainMenuState::GetInstance(), true);
				CSpaceStationState::GetInstance()->iswon = true;
				CHowToPlayState::GetInstance()->SetIsGuideRunning(false);
			}
			else
			{
				CSpaceStationState::GetInstance()->iswon = true;
				CGame::GetInstance()->ChangeState(CSpaceStationState::GetInstance(), true);
			}
			//CGame::GetInstance()->PushState(CSpaceStationState::GetInstance());
		}
		if(m_nCurrentSelection == 1)
		{
			// Exit
			return false;
		}
	}
	return true;
}
void CWinState::Update(float fElapsedTime)
{
	m_pPlayer->SetPosX(m_pPlayer->GetPosX()+0.5f);
	CAnimationManager::GetInstance()->UpdateAnimation(fElapsedTime,m_nPlayerDancingID);
	if(m_pPlayer->GetPosX() > CGame::GetInstance()->GetScreenWidth())
		m_pPlayer->SetPosX(0);

}
void CWinState::Render(void)
{
	CSGD_TextureManager::GetInstance()->Draw(m_nBackgroundImageID, 0, 0, 0.67f, 0.6f);

	CAnimationManager::GetInstance()->Play(int(m_pPlayer->GetPosX()),int(m_pPlayer->GetPosY()),1.5f,1.5f,m_nPlayerDancingID);

	
		if(m_nCurrentSelection == 0)
		{
			if(CHowToPlayState::GetInstance()->GetIsGuideRunning() || CSpaceStationState::GetInstance()->m_bWonTheGame)
			{
				CPrintBitmapFontToScreen::GetInstance()->Print("BACK TO MAIN MENU", 250, 450, 1.0f, D3DCOLOR_XRGB(0,255,0));
			}
			else
			{
				CPrintBitmapFontToScreen::GetInstance()->Print("BACK TO SPACE STATION", 250, 450, 1.0f, D3DCOLOR_XRGB(0,255,0));
			}
			
		}
		else
			if(CHowToPlayState::GetInstance()->GetIsGuideRunning()|| CSpaceStationState::GetInstance()->m_bWonTheGame)
		{
			CPrintBitmapFontToScreen::GetInstance()->Print("BACK TO MAIN MENU", 250, 450, 1.0f, D3DCOLOR_XRGB(255,255,0));
		}
		else
		{
			CPrintBitmapFontToScreen::GetInstance()->Print("BACK TO SPACE STATION", 250, 450, 1.0f, D3DCOLOR_XRGB(255,255,0));
		}


	if(m_nCurrentSelection == 1)
		CPrintBitmapFontToScreen::GetInstance()->Print("EXIT", 100, 450,1.0f, D3DCOLOR_XRGB(0,255,0));
	else
		CPrintBitmapFontToScreen::GetInstance()->Print("EXIT", 100, 450,1.0f, D3DCOLOR_XRGB(255,255,0));


	CPrintBitmapFontToScreen::GetInstance()->Print("YOU WON", 150, 50, 3.0f, D3DCOLOR_XRGB(255,0,0));

}
void CWinState::Exit(void)
{

}